#!/usr/bin/perl -w
#
use strict;

use CGI::Carp qw(fatalsToBrowser);

my $Config = "/usr/local/Captivator/etc/captivator.config";
use lib qw(/usr/local/Captivator/lib/);

use CGI;
use Captivator;
use Captivator::Guests;
use Captivator::CurrentUsers;
use NetAddr::IP;

my $remotehost = matchIpAddr($ENV{'REMOTE_ADDR'});
my $ipobj = new NetAddr::IP($remotehost);
my $remotehost = $ipobj->numeric();

my $user = Captivator::CurrentUsers->search_where(v4ip => {'=',$remotehost})->next;
unless($user) { 
        die("Who are you? Remote IP $remotehost not found in CurrentUsers");
}
my $guest = Captivator::Guests->search({email => $user->username})->next;
unless($guest) { 
	die("Who are you? Guest username not found in Guests table");
}

my $q = new CGI;

if ($q->param('password1') and length($q->param('password1')) > 0 ) {
	if($q->param('confpassword') and $q->param('password1') eq $q->param('confpassword')) {
		$guest->password($q->param('password1'));
		$guest->update;
		print $q->redirect("password_changed.html");
	} else {
		die("Passwords do not match.\n");
	}
} else {
	die("Please enter a new password.\n");
}
